package com.ruoyi.material.service.impl;

import com.ruoyi.audit.mapper.AuditMapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateCheck.mapper.MateCheckMapper;
import com.ruoyi.mateWarehousing.domain.Inventory;
import com.ruoyi.mateWarehousing.service.MateWarehousingService;
import com.ruoyi.material.domain.MateProductDevice;
import com.ruoyi.material.domain.MateProductModel;
import com.ruoyi.material.domain.MaterialProduct;
import com.ruoyi.material.mapper.MateProductMapper;
import com.ruoyi.material.service.MateProductService;
import com.ruoyi.mtAudit.mapper.MtAuditMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

/**
 * @Author
 * @Version 1.0
 */
@Service
public class MateProductServiceImpl implements MateProductService {
    @Autowired
    MateProductMapper mateProductMapper;

    @Autowired
    AuditMapper auditMapper;

    @Autowired
    MateCheckMapper mateCheckMapper;

    @Autowired
    MtAuditMapper mtAuditMapper;

    @Override
    public int insertMateProduct(MaterialProduct materialProduct) {

        try {
            mateProductMapper.insertMateProduct(materialProduct);
            if (materialProduct.getMateProductModel() != null) {
                for (MateProductModel materProductModel : materialProduct.getMateProductModel()) {
                    materProductModel.setMateproductId(materialProduct.getMateproductId());
                    materProductModel.setDeptId(SecurityUtils.getLoginUser().getCompanyId());
                    materProductModel.setComparisonCycle(180); // 对比周期
                    materProductModel.setProcurementCycle(60); // 采购周期
                    mateProductMapper.insertMateProductModel(materProductModel);
                }
            }
            if (materialProduct.getMateProductDevices() != null) {
                for (MateProductDevice mateProductDevice : materialProduct.getMateProductDevices()) {
                    mateProductDevice.setDeptId(SecurityUtils.getDeptId());
                    mateProductDevice.setMateproductId(materialProduct.getMateproductId());
                    mateProductMapper.insertMateProductDevice(mateProductDevice);
                }
            }
        } catch (Exception e) {
            mateProductMapper.deleteMaterialProduct(materialProduct.getMateproductId());
            mateProductMapper.deleteMaterialProductModelById(materialProduct.getMateproductId());
            mateProductMapper.deleteMaterialProductDevice(materialProduct.getMateproductId());
        }

        return 1;
    }

    @Override
    public List<MaterialProduct> selectMateriProduct(MaterialProduct materialProduct) {
        return mateProductMapper.selectMateriProduct(materialProduct);
    }

    @Override
    public int updateMateProduct(MaterialProduct materialProduct) {
        MaterialProduct materialProduct1 = mateProductMapper.selectMateProductById(materialProduct.getMateproductId());
        List<MateProductModel> mateProductModels = mateProductMapper.selectMateProductModel(materialProduct.getMateproductId());
        try {
            if (materialProduct.getMateProductModel() != null || materialProduct.getMateProductModel().size() != 0) {
                ArrayList<Long> arrayList = new ArrayList();
                for (MateProductModel mateProductModel : mateProductModels) {
                    arrayList.add(mateProductModel.getMateModeno());
                }
                ArrayList<Long> productModelList = new ArrayList();
                for (MateProductModel mateProductModel : materialProduct.getMateProductModel()) {
                    mateProductModel.setMateproductId(materialProduct.getMateproductId());
                    Long modelNo = mateProductModel.getMateModeno();
                    if (null == mateProductModel.getComparisonCycle()) {
                        mateProductModel.setComparisonCycle(180);
                    }
                    if (null == mateProductModel.getProcurementCycle()) {
                        mateProductModel.setProcurementCycle(60);
                    }
                    if (mateProductMapper.selectMateProductModelByModeno(modelNo) == 1) {
                        productModelList.add(modelNo);
                        mateProductMapper.updateMateProdcutModel(mateProductModel);
                    } else if (mateProductModel.getMateModelCode() != null) {
                        mateProductModel.setMateproductId(materialProduct.getMateproductId());
                        mateProductMapper.insertMateProductModel(mateProductModel);
                    }
                }

                if (materialProduct.getMateProductDevices() != null) {
                    mateProductMapper.deleteMaterialProductDevice(materialProduct.getMateproductId());
                    for (MateProductDevice mateProductDevice : materialProduct.getMateProductDevices()) {
                        mateProductDevice.setDeptId(SecurityUtils.getDeptId());
                        mateProductDevice.setMateproductId(materialProduct.getMateproductId());
                        mateProductMapper.insertMateProductDevice(mateProductDevice);
                    }
                }

                TreeSet<Long> longs = new TreeSet<>();
                boolean b = arrayList.removeAll(productModelList);
                for (Long aLong : arrayList) {
                    longs.add(aLong);
                }
                //删除数组
                if (longs.size() != 0) {
                    for (Long aLong : longs) {
                        mateProductMapper.deleteMateProductModel(aLong);
                    }
                }
                mateProductMapper.updateMateProduct(materialProduct);
            }
        } catch (Exception e) {
            mateProductMapper.updateMateProduct(materialProduct1);
        }
        return 1;
    }

    @Override
    public List<MaterialProduct> selectProducts(Map<String, Object> resMap) {
        return mateProductMapper.selectProducts(resMap);
    }

    @Override
    public List<MaterialProduct> selectProductLists(MaterialProduct materialProduct) {
        return mateProductMapper.selectProductLists(materialProduct);
    }

    @Override
    public List<MaterialProduct> selectMateProduct(Long deptId) {
        return mateProductMapper.selectMateProduct(deptId);
    }

    @Autowired
    private MateWarehousingService mateWarehousingService;
    @Override
    public List<Inventory> updateModelInfo(MateProductModel mateProductModel) {
        // 修改原材料型号的对比周期和进货周期
        int result = mateProductMapper.updateMateProdcutModel(mateProductModel);
        // 根据产品ID、型号ID、供货商ID查询修改后的对比数据

        Inventory inventory = new Inventory();
        inventory.setMateProductId(mateProductModel.getMateproductId().toString());
        inventory.setMateModeNo(mateProductModel.getMateModeno().toString());
        inventory.setMaterialId(mateProductModel.getMaterialId().toString());
        List<Inventory> inventories = mateWarehousingService.selectProductToWar_new(inventory);

        return inventories;
    }
}
